# Use the official Python image as the base image
FROM python:3.9-slim

# 设置环境变量以使用更快的APT镜像
ARG DEBIAN_FRONTEND=noninteractive
# Install Node.js (required for execjs)
#RUN apt-get update && apt-get install -y curl gnupg && \
#    curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
#    apt-get install -y nodejs && \
#    apt-get clean && rm -rf /var/lib/apt/lists/* \
## 使用Debian官方镜像源，并且更新软件包列表和安装Node.js
#RUN sed -i 's/http:\/\/deb.debian.org/http:\/\/ftp.debian.org/g' /etc/apt/sources.list && \
#    apt-get update && \
#    apt-get install -y --no-install-recommends \
#    curl \
#    gnupg && \
#    curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
#    apt-get install -y nodejs && \
#    apt-get clean && rm -rf /var/lib/apt/lists/*
# 更新APT源为清华大学镜像源，并且更新软件包列表和安装Node.js
RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free" > /etc/apt/sources.list && \
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free" >> /etc/apt/sources.list && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
    curl \
    gnupg && \
    curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
    apt-get install -y nodejs && \
    apt-get clean && rm -rf /var/lib/apt/lists/*




# Set the working directory
WORKDIR /app

# Copy the requirements file to the working directory
COPY requirements.txt ./
COPY scrapy_service/package*.json ./

RUN npm install

# Install the dependencies from requirements.txt
RUN pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
# Copy the Scrapyd configuration file to the appropriate directory
COPY scrapyd.conf /etc/scrapyd/

# Copy the rest of the application to the working directory
COPY . .

# Create directories for Scrapyd
RUN mkdir -p /var/lib/scrapyd/eggs /var/log/scrapyd/logs /var/lib/scrapyd/items /var/lib/scrapyd/dbs

# Expose Scrapyd port
EXPOSE 6800

# Start Scrapyd
CMD ["scrapyd"]
